package com.pro.springbootshop.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.pro.springbootshop.entity.Seller;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SellerTokenUtil {
    private final String privateKey="shopOnline";

    /**
     * 加密token.
     */
    public String getToken(Seller seller) {
        //这个是放到负载payLoad 里面,魔法值可以使用常量类进行封装.
        String token = JWT
                .create()
                .withClaim("sellerId" , seller.getSellerId())
                .withClaim("sellerName", seller.getSellerName())
                .withClaim("sellerAddress", seller.getSellerAddress())
                .withClaim("sellerPhone", seller.getSellerPhone())
                .withClaim("timeStamp", System.currentTimeMillis())
                .sign(Algorithm.HMAC256(privateKey));
        return token;
    }

    /**
     * 解析token.
     * (优化可以用常量固定魔法值+使用DTO在 mvc 之前传输数据，而不是 map,这里因为篇幅原因就不做了)
     * {
     * "userId": "3412435312",
     * "userRole": "ROLE_USER",
     * "timeStamp": "134143214"
     * }
     */
    public Map<String, String> parseToken(String token) {
        HashMap<String, String> map = new HashMap<>();
        DecodedJWT decodedjwt = JWT.require(Algorithm.HMAC256(privateKey))
                .build().verify(token);
        Claim sellerId = decodedjwt.getClaim("sellerId");
        Claim sellerName = decodedjwt.getClaim("sellerName");
        Claim sellerAddress = decodedjwt.getClaim("sellerAddress");
        Claim sellerPhone = decodedjwt.getClaim("sellerPhone");
        Claim timeStamp = decodedjwt.getClaim("timeStamp");
        map.put("sellerId", sellerId.asString());
        map.put("sellerName", sellerName.asString());
        map.put("sellerAddress", sellerAddress.asString());
        map.put("sellerPhone", sellerPhone.asString());
        map.put("timeStamp", timeStamp.asLong().toString());
        return map;
    }
}
